package com.aaa.controller.tester;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.Result;
import com.aaa.entity.mht.TestResultMht;
import com.aaa.entity.tester.TestResult;
import com.aaa.entity.tester.Tester;
import com.aaa.entity.ViewsEntity;
import com.aaa.service.mht.TestResultMhtService;
import com.aaa.service.tester.LoginTestService;
import com.aaa.service.tester.TestResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author ：肖成杰（1752167034@qq.com）
 * @date ：Created in 2022/9/1 19:07
 * @description：登录测试页面
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("test")
public class
LoginTest extends BaseController {

    @Autowired
    private LoginTestService loginTestService;

    @Autowired
    private TestResultService testResultService;


    @Autowired
    private TestResultMhtService testResultMhtService;


    /**
     * @return com.aaa.entity.Result
     * @create by: 肖成杰
     * @description:
     * @create time: 2022/9/2 16:12
     * @params tester  姓名  手机号  邀请码
     * @params session 保存数据
     */
    @RequestMapping("testLogin.do")
    public Result loginTestQues(Tester tester, HttpSession session) {
        //用邀请码查询计划id  id  开始时间testBegin  结束时间testEnd 测试类型type
        List<ViewsEntity> viewsList = loginTestService.queryTestCode(tester);
        if (ObjectUtil.isNotEmpty(viewsList)) {
            Date testEnd = viewsList.get(0).getTestEnd();
            Date testBegin = viewsList.get(0).getTestBegin();
            String type = viewsList.get(0).getType();
            //测试计划id
            Integer id = viewsList.get(0).getId();
            Date date = new Date();
            if (testBegin.after(date)) {
                return fail("测试时间还未开始");
            }
            if (testEnd.before(date)) {

                return fail("测试已结束");
            }
            //将计划id放到tester类中
            tester.setTestPlanId(id);
            for (ViewsEntity viewsEntity : viewsList) {
                //将测试计划id 与测试类型 与测试名称放到session中
                session.setAttribute("view", viewsEntity);
            }

            List<Map<String, Object>> mapList = new ArrayList<>();
            switch (type) {
                case "fpa":
                    List<Integer> listFPA = loginTestService.selectFPAId(tester);
                    if (ObjectUtil.isEmpty(listFPA)) {
                        //将数据插入到tbl_tester_fpa表中
                        loginTestService.insertFPA(tester);
                        //查询测试人员id
                        List<Integer> list = loginTestService.selectFPAId(tester);
                        //将测试人员id放到session中
                        session.setAttribute("testerId", list.get(0));
                        //查询问题表
                        mapList = loginTestService.selectQuestionFPA();
                        session.setAttribute("list", mapList);
                        return success();
                    }
                    break;
                case "scl":
                    if (ObjectUtil.isEmpty(loginTestService.selectSCLId(tester))) {
                        //将数据插入到tbl_tester_scl表中
                        loginTestService.insertSCL(tester);
                        //查询测试人员id
                        List<Integer> listScl = loginTestService.selectSCLId(tester);
                        //将测试人员id放到session中
                        session.setAttribute("testerId", listScl.get(0));
                        //查询问题表
                        mapList = loginTestService.selectQuestionSCL();
                        session.setAttribute("list", mapList);
                        return success();
                    }
                    break;
                case "mht":
                    //根据手机号及状态查询视图中的数据
                    List<TestResultMht> testResultMhts = testResultMhtService.queryByPhone(tester);
                    //用validity 接收  效度评估
                    int validity = 0;
                    if (ObjectUtil.isNotEmpty(testResultMhts)) {
                        //如果存在数据则接收 效度评估
                        validity = Integer.parseInt(testResultMhts.get(0).getValidity() + "");
                        session.setAttribute("tester", tester);
                    }
                    if (ObjectUtil.isEmpty(loginTestService.selectMHTId(tester)) || validity > 7) {
                        /**
                         * 判断手机号是否存在在测试表中 ，或者测试者的得分用validity 是否大于 7
                         */
                        //将数据插入到tbl_tester_mht表中
                        loginTestService.insertMHT(tester);
                        //查询测试人员id  根据手机号和状态查询
                        List<Integer> listMht = loginTestService.selectMHTId(tester);
                        if(listMht.size()>1){
                            testResultService.updateStatus(listMht.get(0));
                            //将测试人员id放到session中
                            session.setAttribute("testerId", listMht.get(1));
                        }else {
                            session.setAttribute("testerId", listMht.get(0));
                        }

                        //查询问题表
                        mapList = loginTestService.selectQuestionMHT();
                        session.setAttribute("list", mapList);
                        return success();
                    }
                    break;
                case "sas":
                    if (ObjectUtil.isEmpty(loginTestService.selectSASId(tester))) {
                        //将数据插入到tbl_tester_sas表中
                        loginTestService.insertSAS(tester);
                        //查询测试人员id
                        List<Integer> listSas = loginTestService.selectSASId(tester);
                        //将测试人员id放到session中
                        session.setAttribute("testerId", listSas.get(0));
                        //查询问题表
                        mapList = loginTestService.selectQuestionSAS();
                        session.setAttribute("list", mapList);
                        return success();
                    }
                    break;
                case "sds":
                    if (ObjectUtil.isEmpty(loginTestService.selectSDSId(tester))) {
                        //将数据插入到tbl_tester_fpa表中
                        loginTestService.insertSDS(tester);
                        //查询测试人员id
                        List<Integer> listSds = loginTestService.selectSDSId(tester);
                        //将测试人员id放到session中
                        session.setAttribute("testerId", listSds.get(0));
                        //查询问题表
                        mapList = loginTestService.selectQuestionSDS();
                        session.setAttribute("list", mapList);
                        return success();
                    }
                    break;
                default:
                    break;
            }
            return fail("手机号已经测试过");
        }
        return fail("邀请码不存在");
    }

    @RequestMapping("saveBatchTestResult.do")
    public Result saveBatchTestResult(@RequestBody List<TestResult> testResult, HttpSession session) {
        ViewsEntity view = (ViewsEntity) session.getAttribute("view");
        switch (view.getType()) {
            case "fpa":
                int insertFPA = testResultService.insertFPA(testResult);
                if (insertFPA > 0) {
                    return success();
                }
                break;
            case "sas":
                int insertSAS = testResultService.insertSAS(testResult);
                if (insertSAS > 0) {
                    return success();
                }
                break;
            case "sds":
                int insertSDS = testResultService.insertSDS(testResult);
                if (insertSDS > 0) {
                    return success();
                }
                break;
            case "mht":
                int insertMHT = testResultService.insertMHT(testResult);
                if (insertMHT > 0) {
                    return success();
                }
                break;
            case "scl":
                int insertSCL = testResultService.insertSCL(testResult);
                if (insertSCL > 0) {
                    return success();
                }
                break;
            default:
                break;
        }
        return fail();
    }

 /*   //用户查询页面
    @RequestMapping("toShowResult.do")
    public ModelAndView toShowUser(ModelAndView modelAndView,HttpSession session){
        Object type = session.getAttribute("type");
        modelAndView.setViewName(type+"/testResult");
        return modelAndView;
    }*/

}



